//
//  AppDelegate.swift
//  MicroBlog
//
//  Created by QT on 15/7/28.
//  Copyright © 2015年 綦敦涛. All rights reserved.
//

import UIKit
let ViewControllerNotification = "ViewControllerNotification"
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        
//        print(UserAccount.loadAccount())
        
        // 添加通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchController:", name: ViewControllerNotification, object: nil)
        
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
    
//        window?.rootViewController = MainViewController()
        window?.rootViewController = entryController()
        
        window?.makeKeyAndVisible()
        
        // 设置全局外观
        setUpAppearance()
        
//        print(isNewVerson())
        
        return true
    }
    
    // 监听到通知执行的方法
    //
    func switchController(notification:NSNotification){
//        print(notification.object)
        let resu = notification.object as! Bool
//        let a = Int(resu)
        window?.rootViewController = resu ?  MainViewController() : WelcomeViewController()
    }
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    // 选择初始化控制器
    private func entryController() -> UIViewController{
        // 判断登录
        if !UserAccount.userLogon{
            return MainViewController()
        }
        // 检查新版本
        return isNewVerson() ? NewFeatureController() : WelcomeViewController()
    }
    
    // 检查新版本
    private func isNewVerson() ->Bool{
        // 1. 获取当前版本
        let currentVerson = Double(NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)
        // 2. 获取旧版本(从偏好设置中获取)
        let sandVersonKey = "sandVersonKey"
        let lastVerson = NSUserDefaults.standardUserDefaults().doubleForKey(sandVersonKey)
        
        // 3. 将新版本写入偏好设置
        NSUserDefaults.standardUserDefaults().setDouble(currentVerson!, forKey: sandVersonKey)
        return currentVerson > lastVerson
    }
    
    
    func setUpAppearance(){
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        UITabBar.appearance().tintColor = UIColor.orangeColor()
    }
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

